package com.example.demo6.Controller;

import com.example.demo6.Service.IGoodsService;
import com.example.demo6.entity.Goods;
import com.example.demo6.utils.PageResult;
import com.example.demo6.utils.PassToken;
import com.example.demo6.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * goodsController
 * 
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询goods列表
     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(Goods goods, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "3")int pageSize)
    {
        PageResult pageResult=goodsService.selectGoodsList(goods,pageNum,pageSize);
        return ServerResponse.createBySuccess(200,pageResult,"查询成功");
    }
    @PassToken
    @GetMapping("/list1")
    public ServerResponse lists(Goods goods)
    {
        List<Goods> list=goodsService.selectGoodList(goods);
        return ServerResponse.createBySuccess(200,list,"查询成功");
    }
    @PassToken
    @GetMapping("/cid/{cid}")
    public ServerResponse getGoodsBycid(@PathVariable Long cid)
    {
        List<Goods> list=goodsService.selectGoodsByCid(cid);
        return ServerResponse.createBySuccess(200,list,"查询成功");
    }

    /**
     * 导出goods列表
     */
//    @PostMapping("/export"),

//    public void export(HttpServletResponse response, Goods goods)
//    {
//        List<Goods> list = goodsService.selectGoodsList(goods);
//        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
//        util.exportExcel(response, list, "goods数据");
//    }

    /**
     * 获取goods详细信息
     */
    @PassToken
    @GetMapping(value = "/{gid}")
    public ServerResponse getInfo(@PathVariable("gid") Long gid)
    {
        Goods goods=goodsService.selectGoodsByGid(gid);
        return ServerResponse.createBySuccess(200,goods,"查询成功");
    }

    /**
     * 新增goods
     */
    @PostMapping
    public ServerResponse add(@RequestBody Goods goods)
    {
        int i=goodsService.insertGoods(goods);
        if(i>0){
            return ServerResponse.createBySuccess(200,"","添加成功");
        }else{
            return ServerResponse.createByFail(500,"添加失败");
        }
    }

    /**
     * 修改goods
     */
    @PutMapping
    public ServerResponse edit(@RequestBody Goods goods)
    {
        int i=goodsService.updateGoods(goods);
        if(i>0){
            return ServerResponse.createBySuccess(200,"","更新成功");
        }else{
            return ServerResponse.createByFail(500,"更新失败");
        }
    }

    /**
     * 删除goods
     */
	@DeleteMapping("/{gids}")
    public ServerResponse remove(@PathVariable Long[] gids)
    {
        int i=goodsService.deleteGoodsByGids(gids);
        if(i>0){
            return ServerResponse.createBySuccess(200,"","删除成功");
        }else {
            return ServerResponse.createByFail(500, "删除失败");
        }
    }
}
